package cn.modoumama.notice.util;

import java.lang.annotation.Annotation;
import java.util.Map;
import java.util.Map.Entry;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.util.StringUtils;

import cn.modoumama.notice.annotation.NoticeApi;
import cn.modoumama.notice.entity.ApiMethodModel;
import cn.modoumama.notice.service.ApiMethodService;

public class NoticeApiServiceScannerConfigurer implements ApplicationContextAware,InitializingBean {
	protected static final Logger logger = LoggerFactory.getLogger(NoticeApiServiceScannerConfigurer.class);
	private ApplicationContext applicationContext;
	BeanDefinitionRegistry registry;
	private String basePackage = "";
	@Autowired
	private ApiMethodService apiMethodService;
	private Class<? extends Annotation> typeAnnotationType;
	private Class<? extends NoticeApi> methodAnnotationType;
	private String baseUrl = "";

	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		this.applicationContext = applicationContext;
	}

	public String getBasePackage() {
		return basePackage;
	}

	public void setBasePackage(String basePackage) {
		this.basePackage = basePackage;
	}
	
	public ApiMethodService getApiMethodService() {
		return apiMethodService;
	}

	public void setApiMethodService(ApiMethodService apiMethodService) {
		this.apiMethodService = apiMethodService;
	}

	public Class<? extends Annotation> getTypeAnnotationType() {
		return typeAnnotationType;
	}

	public void setTypeAnnotationType(Class<? extends Annotation> typeAnnotationType) {
		this.typeAnnotationType = typeAnnotationType;
	}

	public Class<? extends Annotation> getMethodAnnotationType() {
		return methodAnnotationType;
	}

	public void setMethodAnnotationType(Class<? extends NoticeApi> methodAnnotationType) {
		this.methodAnnotationType = methodAnnotationType;
	}

	public final String getBaseUrl() {
		return baseUrl;
	}

	public void setBaseUrl(String baseUrl) {
		this.baseUrl = baseUrl;
	}

	public void afterPropertiesSet() throws Exception {
		//扫描类
		registry = new DefaultListableBeanFactory();
		ClassPathNoticeApiServiceScanner scanner = new ClassPathNoticeApiServiceScanner(registry);
		scanner.setTypeAnnotationType(typeAnnotationType);
		if(methodAnnotationType == null){
			methodAnnotationType = NoticeApi.class;
		}
		scanner.setMethodAnnotationType(methodAnnotationType);
		scanner.setBaseUrl(baseUrl);
		scanner.setResourceLoader(this.applicationContext);
		scanner.registerFilters();
		scanner.scan(StringUtils.tokenizeToStringArray(basePackage, ",; \t\n"));
		
		//修改接口信息
		Map<String, ApiMethodModel> apiMethods = ApiMethodUtils.getApiMethods();
		for (Entry<String, ApiMethodModel> apiMethodEntry : apiMethods.entrySet()) {
			ApiMethodModel apiMethodModel = apiMethodEntry.getValue();
			//修改接口方法信息
			apiMethodService.mergeMethod(apiMethodModel);
		}
	}

}
